#include "stdafx.h"
#include "log.h"

void xLog(std::wstring str)
{
    HMODULE hModule = ::GetModuleHandle(NULL);
    WCHAR strFullPath[MAX_PATH] = {0};
    ::GetModuleFileName(hModule, strFullPath, MAX_PATH);
    strFullPath[MAX_PATH - 1] = L'\0';
    PathRemoveFileSpec(strFullPath);

    std::wstring strLog(strFullPath);
    strLog.append(_T("\\log.txt"));

    HANDLE hFile = ::CreateFile(strLog.c_str(), GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile != INVALID_HANDLE_VALUE)
    {
        SetFilePointer(hFile, 0, NULL, FILE_END); 
        DWORD dwNum = 0;

        str += _T("\r\n");

        ::WriteFile(hFile, str.c_str(), (DWORD)str.length(), &dwNum, NULL);
        ::CloseHandle(hFile);
    }
}